"use client"

export interface User {
  id: string
  email: string
  name: string
  role: "admin" | "user"
  createdAt: string
}

export interface AuthState {
  user: User | null
  isAuthenticated: boolean
}

// Mock authentication - in production, use real backend
export const authService = {
  login: async (email: string, password: string): Promise<User> => {
    // Simulate API call
    await new Promise((resolve) => setTimeout(resolve, 500))

    // Mock validation
    if (email && password) {
      const user: User = {
        id: Math.random().toString(36).substr(2, 9),
        email,
        name: email.split("@")[0],
        role: email.includes("admin") ? "admin" : "user",
        createdAt: new Date().toISOString(),
      }

      localStorage.setItem("user", JSON.stringify(user))
      localStorage.setItem("token", "mock-jwt-token")
      return user
    }

    throw new Error("Invalid credentials")
  },

  register: async (email: string, password: string, name: string): Promise<User> => {
    // Simulate API call
    await new Promise((resolve) => setTimeout(resolve, 500))

    const user: User = {
      id: Math.random().toString(36).substr(2, 9),
      email,
      name,
      role: "user",
      createdAt: new Date().toISOString(),
    }

    localStorage.setItem("user", JSON.stringify(user))
    localStorage.setItem("token", "mock-jwt-token")
    return user
  },

  logout: () => {
    localStorage.removeItem("user")
    localStorage.removeItem("token")
  },

  getCurrentUser: (): User | null => {
    if (typeof window === "undefined") return null
    const userStr = localStorage.getItem("user")
    return userStr ? JSON.parse(userStr) : null
  },

  isAuthenticated: (): boolean => {
    if (typeof window === "undefined") return false
    return !!localStorage.getItem("token")
  },
}
